// 常量不变
var floorCount = 21; // 楼层数
var doorTime = 1000; // 开门时间
var handlePeople = 12; // 电梯最高人数
// var idleTime = 600;

var doorDuration = doorTime;
// var idleDuration = idleTime;

var elevator = {
	current: 1, // 当前楼层
	status: 0,  // 0: 停止, 1: 升, 2: 降
	doorOpened: false,
	people: 0,
};

var floorBtn = [];

// 初始化
$(document).ready(function(){
	var insiderBtnRowAt = floorCount%2;
	$("#well").css("height", floorCount*36+"px");
	$("#insider").css("height", floorCount*36+"px");
	for (var i = floorCount; i > 0; i--) {
		// 初始化楼层按钮状态
		floorBtn.push({
			up: false,
			down: false,
			go: false
		});

		// 渲染楼层上下按键
		var htmlTo = "<div class='btnsRow'><div style='float: left; width: 30px;'>"+i+"</div>";
		if (i != floorCount){
			htmlTo += "<a href='javascript:;'><div class='btn upBtn' floor='"+i+"' acc='1'>▲</div></a>";
		}
		if (i != 1) {
			htmlTo += "<a href='javascript:;'><div class='btn downBtn' floor='"+i+"' acc='2'>▼</div></a>";
		}
		$("#btnsGroup").append(htmlTo+"</div>");

		$("#downer").css("visibility", "hidden");
		$("#uper").css("visibility", "hidden");

		// 渲染电梯内部按键
		if (i % 2 == insiderBtnRowAt) {
			$("#insider").append("<div class='btnsRow'><a href='javascript:;'><div class='btn' floor='"+i+"' acc='0'>"+i+"</div></a>");
			if (i-1 != 0) {
				$("#insider").append("<a href='javascript:;'><div class='btn' floor='"+(i-1)+"' acc='0'>"+(i-1)+"</div></a>");
			}
			$("#insider").append("</div>");
		}

	}

	// 开关门按钮
	$("#insider").append("<div class='btnsRow'></div>");
	$("#insider").append("<div class='btnsRow'><a href='javascript:;'><div class='btn' id='openDoor'>&lt;|&gt;</div></a><a href='javascript:;'><div class='btn' id='closeDoor'>&gt;|&lt;</div></a></div>");

	// 报警按钮
	$("#insider").append("<div class='btnsRow'><a href='javascript:;'><div class='btn' id='emergency'>&nbsp;报警&nbsp;</div></a></div>");
	$("#insider").append("<div class='btnsRow'>人数: <span id='peopleNum'>0/"+handlePeople+"</span></div>");
	$("#insider").append("<div class='btnsRow' id='peopleControl' style='display:none'><a href='javascript:;'><div class='btn' id='addPeople'>+</div></a><a href='javascript:;'><div class='btn' id='minusPeople'>-</div></a></div>");

	// 按钮点击事件
	$(".btn[acc='0']").click(function(){
		$(this).css("color", "red");
		addTask($(this).attr("floor"));
	});

	$(".btn[acc='1']").click(function(){
		$(this).css("color", "red");
		addUpTask($(this).attr("floor"));
	});
	$(".btn[acc='2']").click(function(){
		$(this).css("color", "red");
		addDownTask($(this).attr("floor"));
	});

	$("#openDoor").click(function(){
		if (elevator.doorOpened) {
			doorDuration = doorTime;
		}else{
			doorOpen(elevator.current, 0, function(){});
		}
		
	});

	$("#closeDoor").click(function(){
		if (elevator.doorOpened) {
			doorDuration = 0;
		}
	});

	$("#emergency").click(function(){
		alert("报警");
	})

	$("#addPeople").click(function(){
		elevator.people ++;
		if (doorDuration < 500) {
			doorDuration = 500;
		}
		$("#peopleNum").html(elevator.people+"/"+handlePeople);
		if (elevator.people > handlePeople) {
			$("#alarm").css("visibility", "visible");
		}else{
			$("#alarm").css("visibility", "hidden");
		}
	})

	$("#minusPeople").click(function(){
		if (elevator.people > 0) {
			elevator.people --;
			if (doorDuration < 500) {
				doorDuration = 500;
			}
		}
		$("#peopleNum").html(elevator.people+"/"+handlePeople);
		if (elevator.people > handlePeople) {
			$("#alarm").css("visibility", "visible");
		}else{
			$("#alarm").css("visibility", "hidden");
		}
	})

	// 初始化电梯状态
	$("#elevator").css("top", (floorCount - elevator.current)*36 + "px");
});

// 电梯内按钮事件
function addTask(floor){
	if (elevator.current == floor) {
		if (!elevator.doorOpened) {
			if (elevator.status == 0) {
				doorOpen(floor, 0, function(){
					continueUp();
				});
			}else{

			}
		}else{
			$(".btn[floor='"+floor+"']").css("color", "black");
		}
	}else{
		floorBtn[floor-1].go = true;
		if (elevator.status == 0 && !elevator.doorOpened) {
			if (elevator.current < floor) {
				moveUp();
			}else{
				moveDown();
			}
		}
	}
}

// 上升按钮事件
function addUpTask(floor){
	if (elevator.current == floor) {
		if (!elevator.doorOpened) {
			if (elevator.status == 0) {
				elevator.status = 1;
				$("#uper").css("visibility", "visible");
				$("#downer").css("visibility", "hidden");
				doorOpen(floor, 1, function(){
					continueUp();
				});
			}else{
			}
		}else{
			floorArrived(floor, 1);
		}
	}else{
		floorBtn[floor-1].up = true;
		if (elevator.status == 0 && !elevator.doorOpened) {
			if (elevator.current < floor) {
				moveUp();
			}else{
				moveDown();
			}
		}
	}
}

// 下降按钮事件
function addDownTask(floor){
	if (elevator.current == floor) {
		if (!elevator.doorOpened) {
			if (elevator.status == 0) {
				elevator.status = 2;
				$("#uper").css("visibility", "hidden");
				$("#downer").css("visibility", "visible");
				doorOpen(floor, 2, function(){
					continueDown();
				});
			}else{
			}
		}else{
			floorArrived(floor, 2);
		}
	}else{
		floorBtn[floor-1].down = true;
		if (elevator.status == 0 && !elevator.doorOpened) {
			if (elevator.current < floor) {
				moveUp();
			}else{
				moveDown();
			}
		}
	}
}

// 上升一层
function moveUp(){
	elevator.status = 1;
	$("#uper").css("visibility", "visible");
	$("#downer").css("visibility", "hidden");

	$("#elevator").animate({
		top: '-=36px'
	}, 1000, function(){
		elevator.current ++;
		$("#floorNum").html(elevator.current);
		if (elevator.current < floorCount) {
			if (floorBtn[elevator.current-1].up || floorBtn[elevator.current-1].go) {
				doorOpen(elevator.current, 1, function(){
					continueUp();
				});
			}else{
				continueUp();
			}
		}else{
			if (floorBtn[elevator.current-1].down || floorBtn[elevator.current-1].go) {
				elevator.status = 2;
				$("#downer").css("visibility", "visible");
				$("#uper").css("visibility", "hidden");
				doorOpen(elevator.current, 2, function(){
					continueDown();
				});
			}else{
				continueDown();
			}
		}
		
	});
}

function continueUp(){
	var nextState = false;
	// 判断是否还有向上的
	for (var i = elevator.current+1; i <= floorCount; i++) {
		if (floorBtn[i-1].up || floorBtn[i-1].go) {
			moveUp();
			nextState = true;
			break;
		}
	}
	// 否则就判断楼上有没有要向下的
	if (!nextState) {
		for (var i = elevator.current+1; i <= floorCount; i++) {
			if (floorBtn[i-1].down) {
				moveUp();
				nextState = true;
				break;
			}
		}
	}
	// 再否则就判断楼下的按钮
	if (!nextState) {
		for (var i = elevator.current-1; i >= 1; i--) {
			if (floorBtn[i-1].up || floorBtn[i-1].go || floorBtn[i-1].down) {
				moveDown();
				nextState = true;
				break;
			}
		}
	}
	// 都没有就停下
	if (!nextState) {
		moveStop();
	}
}

// 下降 跟上升类似 反转了
function moveDown(){
	elevator.status = 2;
	$("#downer").css("visibility", "visible");
	$("#uper").css("visibility", "hidden");

	$("#elevator").animate({
		top: '+=36px'
	}, 1000, function(){
		elevator.current --;
		$("#floorNum").html(elevator.current);
		if (elevator.current > 1) {
			if (floorBtn[elevator.current-1].down || floorBtn[elevator.current-1].go) {
				doorOpen(elevator.current, 2, function(){
					continueDown();
				});
			}else{
				continueDown();
			}
		}else{
			if (floorBtn[elevator.current-1].up || floorBtn[elevator.current-1].go) {
				elevator.status = 1;
				$("#uper").css("visibility", "visible");
				$("#downer").css("visibility", "hidden");
				doorOpen(elevator.current, 1, function(){
					continueUp();
				});
			}else{
				continueUp();
			}
		}
		
	});
}

function continueDown(){
	var nextState = false;
	for (var i = elevator.current-1; i >= 1; i--) {
		if (floorBtn[i-1].down || floorBtn[i-1].go) {
			moveDown();
			nextState = true;
			break;
		}
	}
	if (!nextState) {
		for (var i = elevator.current-1; i >= 1; i--) {
			if (floorBtn[i-1].up) {
				moveDown();
				nextState = true;
				break;
			}
		}
	}
	if (!nextState) {
		for (var i = elevator.current+1; i <= floorCount; i++) {
			if (floorBtn[i-1].up || floorBtn[i-1].go || floorBtn[i-1].down) {
				moveDown();
				nextState = true;
				break;
			}
		}
	}
	if (!nextState) {
		moveStop();
	}
}

// 停止(没有任务了)
function moveStop(){
	elevator.status = 0;
	$("#downer").css("visibility", "hidden");
	$("#uper").css("visibility", "hidden");
	// idleDuration = idleTime;
	// idleTimer();
}

// 开门(暂停 恢复之后要继续任务)
function doorOpen(floor, ste, finish){
	floorArrived(floor, ste);
	elevator.doorOpened = true;
	doorDuration = doorTime;
	$("#doorLeft").animate({
		marginLeft: "-50px"
	}, 1000, function(){
		$("#peopleControl").css("display", "block");
		timer(finish);
	})
	$("#doorRight").animate({
		marginRight: "-50px"
	}, 1000);
}

function timer(finish){
	setTimeout(function(){
		doorDuration --;
		
		if (doorDuration <= 0) {
			if (elevator.people < handlePeople) {
				$("#peopleControl").css("display", "none");
				$("#doorLeft").animate({
					marginLeft: "0px"
				}, 1000, function(){
					elevator.doorOpened = false;
					finish();
				});
				$("#doorRight").animate({
					marginRight: "0px"
				}, 1000);
			}else{
				timer(finish);
			}
			
		}else{
			timer(finish);
		}
	}, 1);
}

// function idleTimer(){
// 	setTimeout(function(){
// 		idleDuration --;
// 		// $("#output").html(idleDuration);
// 		if (elevator.status == 0 && elevator.current != 1) {
// 			if (idleDuration <= 0) {
// 				floorBtn[0].go = true;
// 				moveDown();
// 			}else{
// 				idleTimer();
// 			}
// 		}
// 	}, 1)
// }

// 按钮熄灭用的
function floorArrived(floor, ste=0){
	$(".btn[floor='"+floor+"'][acc=0]").css("color", "black");
	floorBtn[floor-1].go = false;
	if (ste == 1) {
		floorBtn[floor-1].up = false;
		$(".btn[floor='"+floor+"'][acc=1]").css("color", "black");
	}
	if (ste == 2) {
		floorBtn[floor-1].down = false;
		$(".btn[floor='"+floor+"'][acc=2]").css("color", "black");
	}
	
}
